window.addEventListener('load', function () {
  //获取用户相关信息 start
  localStorage.setItem('gologin', '请先登录')
  localStorage.setItem('zc', '免费注册')
  const token = localStorage.getItem('xtxtoken')
  var gologin = document.querySelector('.gologin')
  var zc = document.querySelector('.zc')
  var uNe = document.querySelector('.une')
  gologin.innerHTML = localStorage.getItem('gologin')
  zc.innerHTML = localStorage.getItem('zc')
  uNe.innerHTML = '默认用户名'
  if (token) {
    axios({
      method: 'GET',
      url: 'http://big-event-vue-api-t.itheima.net/my/userinfo',
      headers: {
        Authorization: token,
      },
    })
      .then(({ data: res }) => {
        if (res.code !== 0) {
          alert('获取用户信息错误')
        } else {
          localStorage.setItem('gologin', res.data.nickname)
          localStorage.setItem('zc', '退出登录')
          gologin.innerHTML = res.data.nickname || res.data.username
          zc.innerHTML = '退出登录'
          uNe.innerHTML = res.data.nickname || res.data.username
        }
      })
      .catch((error) => {
        console.log(error)
        alert('用户身份信息过期,请重新登录')
        location.href = './login.html'
        localStorage.removeItem('xtxtoken')
      })
  }
  gologin.addEventListener('click', function () {
    if (localStorage.getItem('gologin') == '请先登录') {
      location.href = './login.html'
    } else {
      location.href = './personal.html'
    }
  })
  zc.addEventListener('click', function () {
    if (localStorage.getItem('zc') == '免费注册') {
      location.href = './register.html'
    } else {
      location.href = './login.html'
      localStorage.removeItem('xtxtoken')
    }
  })
  //获取用户相关信息 end
})
